package com.jade.linkong.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jade.linkong.mapper.LkPenaltiesMapper;
import com.jade.linkong.domain.LkMatterProject;
import com.jade.linkong.domain.LkPenalties;
import com.jade.linkong.service.ILkMatterProjectService;
import com.jade.linkong.service.ILkPenaltiesService;
import com.jade.common.core.text.Convert;

/**
 * 考核Service业务层处理
 * 
 * @author jade
 * @date 2020-07-15
 */
@Service
public class LkPenaltiesServiceImpl implements ILkPenaltiesService 
{
    @Autowired
    private LkPenaltiesMapper lkPenaltiesMapper;

    @Autowired
	private ILkMatterProjectService lkMatterProjectService;

    /**
     * 查询考核
     * 
     * @param lkPenaltiesId 考核ID
     * @return 考核
     */
    @Override
    public LkPenalties selectLkPenaltiesById(Long lkPenaltiesId)
    {
        return lkPenaltiesMapper.selectLkPenaltiesById(lkPenaltiesId);
    }

    /**
     * 查询考核列表
     * 
     * @param lkPenalties 考核
     * @return 考核
     */
    @Override
    public List<LkPenalties> selectLkPenaltiesList(LkPenalties lkPenalties)
    {
        return lkPenaltiesMapper.selectLkPenaltiesList(lkPenalties);
    }

    /**
     * 新增考核
     * 
     * @param lkPenalties 考核
     * @return 结果
     */
    @Override
    public int insertLkPenalties(LkPenalties lkPenalties)
    {
        return lkPenaltiesMapper.insertLkPenalties(lkPenalties);
    }

    /**
     * 修改考核
     * 
     * @param lkPenalties 考核
     * @return 结果
     */
    @Override
    public int updateLkPenalties(LkPenalties lkPenalties)
    {
        return lkPenaltiesMapper.updateLkPenalties(lkPenalties);
    }

    /**
     * 删除考核对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteLkPenaltiesByIds(String ids)
    {
        return lkPenaltiesMapper.deleteLkPenaltiesByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除考核信息
     * 
     * @param lkPenaltiesId 考核ID
     * @return 结果
     */
    public int deleteLkPenaltiesById(Long lkPenaltiesId)
    {
        return lkPenaltiesMapper.deleteLkPenaltiesById(lkPenaltiesId);
    }


    @Transactional
	@Override
	public void jobAddOrUpdate(LkMatterProject lkMatterProject) {
		
		//TODO 加类型，如果是企业走企业的信用评分，部门走部门的信用评分。
		
		//根据事项查询，如果有值，扣0.3分，没有是0.2分
		//2、判断是否1次超时还是二次超时
				//3、扣分,并且修改事项表已经扣分
				//4、记录分数表。
		LkPenalties lkPenalties=new LkPenalties();
		
		lkPenalties.setLkProjectId(lkMatterProject.getProjectId());
		lkPenalties.setLkMatterProjectId(lkMatterProject.getId());
		List<LkPenalties> selectLkPenaltiesList = lkPenaltiesMapper.selectLkPenaltiesList(lkPenalties);
		if(CollectionUtils.isEmpty(selectLkPenaltiesList)){
			lkPenalties.setPoint(2);
		}else{
			lkPenalties.setPoint(3);
		}
		
		//TODO 等更新，放责任单位的值，
	//	lkPenalties.setUserId(lkMatterProject.getMainOrg());
		//lkPenalties.setUserName(userName);
		
		lkPenaltiesMapper.insertLkPenalties(lkPenalties);
		LkMatterProject tempP=new LkMatterProject();
		tempP.setId(lkMatterProject.getId());
		tempP.setIsPenalties("2");
		lkMatterProjectService.updateLkMatterProject(tempP);
	}
}
